package com.single.dao;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.base.dao.TemplateDaoImpl;
import com.data.entity.StateMin;
@Repository("singleDao")
public class SingleDaoImpl extends TemplateDaoImpl<StateMin, Integer>implements SingleDao{
	/**
	 * 根据设备id，设备状态，归属时间查询每小时设备状态记录
	 * @param equId
	 * @param stateType
	 * @param belongTime
	 * @return
	 */
	@Override
	public int getDurationByState(int equId, String stateType,String belongTime) {
		//COALESCE函数，duration为null时返回0
		String sql="select  COALESCE(SUM(duration),0)  from stateMin where equId=:equId   and stateType=:stateType   and CONVERT(VARCHAR(13),belongTime,120)=:belongTime";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("equId", equId);
		params.put("stateType", stateType);
		params.put("belongTime", belongTime);
		return jt.queryForObject(sql.toString(), params,Integer.class);
	}
	/**
	 * 根据设备id，设备状态，时间段查询不同设备状态记录持续时间
	 * @param equId
	 * @param stateType
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@Override
	public int getDurationByDate(int equId, String stateType, String startTime,String endTime) {
		String sql="select  COALESCE(SUM(duration),0)  from stateMin where equId=:equId   and stateType=:stateType   and CONVERT(VARCHAR(19),belongTime,120)>=:startTime  and CONVERT(VARCHAR(19),belongTime,120)<:endTime";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("equId", equId);
		params.put("stateType", stateType);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		return jt.queryForObject(sql.toString(), params,Integer.class);
	}
}
